﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Helpers;
using System.Windows.Controls;
using System.ComponentModel.Composition;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using Interfaces;
using System.Windows.Input;
using System.Windows;

namespace xLibris
{
    [Export(typeof(MenuViewModel))]
    public class MenuViewModel : NotifyPropertyChangedBase
    {

        [ImportMany(typeof(IModule))]
        IEnumerable<Lazy<IModule, IModuleMetadata>> _modules;

        UserControl _appView;
        [Export("AppView")]
        public UserControl AppView
        {
            get { return _appView; }
            set
            {
                _appView = value;
                NotifyPropertyChanged("AppView");
            }
        }

        MenuElement _currentMenu;
        public MenuElement CurrentMenu
        {
            get { return _currentMenu; }
            set
            {
                _currentMenu = value;
                NotifyPropertyChanged("CurrentMenu");
                NotifyPropertyChanged("CloseVisibility");
            }
        }

        string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        public Visibility CloseVisibility
        {
            get
            {
                if (CurrentMenu == null || CurrentMenu.Parent == null)
                    return Visibility.Hidden;
                return Visibility.Visible;
            }
        }

        RelayCommand _closeCommand;
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(param => this.Close(),
                        param => this.CanClose);
                }
                return _closeCommand;
            }
        }

        void Close()
        {
            CurrentMenu = CurrentMenu.Parent;
        }

        bool CanClose { get { return true; } }

        MenuElement rootMenu;

        public void Expand(MenuElement me)
        {
            if (me.Children != null)
            {
                foreach (var submenu in me.Children)
                    Expand(submenu);

                var apps = _modules.Where(x => x.Metadata.Group == me.Name);
                me.Children.AddRange(apps
                    .Select(x => new MenuElement
                    {
                        Owner = this,
                        Name = x.Metadata.Name,
                        IconFilename = x.Metadata.IconFilename,
                        Parent = me,
                        Children = null,
                        App = x
                    })
                );
            }
        }

        public void LoadFromFile(string filename)
        {
            XDocument doc = XDocument.Load(filename);
            XElement root = doc.Element("main");

            rootMenu = new MenuElement();
            rootMenu.ParseMenu(this, null, root);
            rootMenu.Name = "Main";
            Expand(rootMenu);

        }

        public MenuViewModel()
        {
        }

        public UserControl View()
        {
            LoadFromFile(@"data\menu.xml");
            CurrentMenu = rootMenu;
            Name = "Main Menu";
            var v = new MenuView();
            v.DataContext = this;
            return v;
        }
    }
}
